package main

import (
	"fmt"
	"log"
	//"os"
	//"ycxp/main2/rabbitmq"
	"github.com/streadway/amqp"
)

/*


./consumer "#" info.payment.* *.log debug.payment.#

*/

func main() {
	//mq := &rabbitmq.RabbitMq{
	//	AmqpUrl:"amqp://yxz:yxz@localhost:5672/ys_vhost",       string //amqp地址
	//	ConnectionNum: 1,    //连接数
	//	ChannelNum:    10,    //每个连接的channel数量
	//}
	//mq.InitAmqp()
	RabbitUrl := fmt.Sprintf("amqp://%s:%s@%s:%d/%s", "yxz", "yxz", "localhost", 5672, "ys_vhost")
	conn, err := amqp.Dial(RabbitUrl)
	failOnError(err, "Failed to connect to RabbitMQ")
	defer conn.Close()

	forever := make(chan bool)

	ch, err := conn.Channel()
	failOnError(err, "Failed to open a channel")
	defer ch.Close()

	topics := [...]string{"ys.msg.aaa"}
	//topics := os.Args[1:]
	topicsCnt := len(topics)

	for routing := 0; routing < topicsCnt; routing++ {
		go func(routingNum int) {
			msgs, err := ch.Consume(
				"ys.queue",
				//"ys.queue",
				"",
				true, //Auto Ack
				false,
				false,
				false,
				nil,
			)

			failOnError(err, "Failed to register a consumer")

			for msg := range msgs {
				log.Printf("In %s consume a message: %s\n", topics[routingNum], msg.Body)
			}

		}(routing)
	}

	<-forever
}

func failOnError(err error, msg string) {
	if err != nil {
		fmt.Printf("%s: %s\n", msg, err)
	}
}
